﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Flash长连接</title>
    <script src="swf/swfobject.js" type="text/javascript"></script>
    <script src="js/lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="js/lib/json/json2.js" type="text/javascript"></script>    
    <script src="swf/XJNet.js" type="text/javascript"></script>
    <script type="text/javascript">
        function OnBodyLoad(o)
        {
            InitXJNet();
        }
        function InitXJNet()
        {
            var vtime = 0;
            var timeInterval;
            /******************************创建HTML元素结束***********************************/
            var DivSwfContent = document.createElement("div");
            DivSwfContent.id = "SwfContent";

            var DivSwfContainer = document.createElement("div");
            DivSwfContainer.id = "SwfContainer";
            DivSwfContainer.appendChild(DivSwfContent);
            document.body.appendChild(DivSwfContainer);
            /******************************创建HTML元素结束***********************************/
            XJNet.IsWriteLog = true; //是否允许XJNet库输出日志
            BuildCtrl(XJNet.IsWriteLog);
            XJNet.LogctrlID = "DetailLog";
            XJNet.LogctrlDebugID = "DebugDetailLog";
            XJNet.IPEndPoint.WebDppIp = "127.0.0.1"; //服务端IP
            XJNet.IPEndPoint.WebDppPort = "45000"; //服务端端口

            XJNet.OnLoad = function()//swf对象初始化完成事件
            {
                setTimeout(function()
                {
                    XJNet.ConnectFlag = XJNet.Connect(XJNet.IPEndPoint);
                    XJNet.MsgLog("Loaded:ConnectFlag = " + XJNet.ConnectFlag);
                }, 4000);
                
            }

            XJNet.OnConnecting = function()//连接之前触发的事件
            {
                vtime = 0;
                timeInterval = setInterval(function()
                {
                    vtime++;
                    if (vtime == 60)
                    {
                        alert('连接超时！');
                        clearInterval(timeInterval);
                    }
                }, 1000);
            };
            XJNet.OnConnected = function()//连接完成事件，可以在这里面给服务器发消息或在其后发最佳
            {
                XJNet.MsgLog("连接已建立");
                clearInterval(timeInterval);
                var msg = new Object();
                msg.MSGID = "3001";
                msg.CONTENT = JSON.stringify({ USERID: "jy", OPTION: "0", TYPE: "0" });

                var msg1 = new Object();
                msg1.MSGID = "3002";
                msg1.CONTENT = JSON.stringify({ SSTATION: "7104", ESTATION: "7106", TRANSITION: "", RATE: "2M", ISSHORT: "0" });
                try
                {
                    //var strRegModel = JSON.stringify(msg);
                    //XJNet.Send(strRegModel);
                    //XJNet.MsgLog("发送了注册消息：" + strRegModel);

                    //var search = JSON.stringify(msg1);
                    //XJNet.Send(search);
                    //XJNet.MsgLog("发送了搜索消息：" + search);
                    
                    //每秒发送一个随机数模拟持续发送消息
                    var tStr;
                    var tInterval = setInterval(function()
                    {
                        tStr = Math.random().toString();
                        XJNet.Send(tStr);
                        XJNet.MsgLog("发送了消息：" + tStr);
                    }, 1000);                    
                }
                catch (e)
                {
                    XJNet.MsgLog(e.message);
                }
            };
            XJNet.OnReceive = function(jsonstr)//接收到服务端发送的数据触发的事件
            {
                XJNet.MsgLog("接收到数据：" + jsonstr);
                //var msg = '{"MSGID":"3002","CONTENT":"{\"SSTATION\":\"7104\",\"ESTATION\":\"7106\",\"TRANSITION\":\"\",\"RATE\":\"2M\",\"ISSHORT\":\"0\"}"}';
                //XJNet.Send(msg);
            }
            XJNet.RegistSwfObject("swf/SocketComponent.swf", "SwfContent");
            function BuildCtrl(isWriteLog)
            {
                if (isWriteLog)
                {
                    var debugLogCtrl = document.createElement('textarea');
                    debugLogCtrl.id = "DetailLog";
                    debugLogCtrl.cols = "50";
                    debugLogCtrl.rows = "10";
                    debugLogCtrl.style.width = "49%";

                    var logCtrl = document.createElement('textarea');
                    logCtrl.id = "DebugDetailLog";
                    logCtrl.cols = "50";
                    logCtrl.rows = "10";
                    logCtrl.style.width = "49%";


                    var logContainer = document.createElement('div');
                    logContainer.id = "LogContainer";
                    logContainer.style.float = "inherit";
                    logContainer.appendChild(debugLogCtrl);
                    logContainer.appendChild(logCtrl);

                    document.body.appendChild(logContainer);
                }
                else
                {
                    if (document.getElementById("LogContainer")) document.removeChild(document.getElementById("LogContainer"));
                }
            }
            //document.getElementById("SwfContainer").style.display = "none";
        }
    </script>
</head>
<body onload="OnBodyLoad(this)">
    <!--main_layout-->
    <div id="main_layout">
        <h1>Flash长连接实现实时推送</h1>
        <div id="swfContainer">
            <h1>Alternative content</h1>
			<!--<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>-->
        </div>
    </div><!--end main_layout-->
    <!--log-layout-->
    <!--<div id="log_layout">
        <textarea id="DetailLog" cols="50" rows="10"></textarea>
        <textarea id="DebugDetailLog" cols="50" rows="10"></textarea>
    </div>-->
    <!--end log_layout-->
</body>
</html>
